package com.example.homework3jpa.controller;


import com.example.homework3jpa.entity.Product;
import com.example.homework3jpa.repository.ProductRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductRepository productRepository;

    @RequestMapping("/list")
    public List<Product> findAll() {
        return productRepository.findAll();
    }

    @RequestMapping("/{id}")
    public Product findOneById(@PathVariable("id") int id) {
        return productRepository.findById(id).get();
    }

    @RequestMapping("/delete/{id}")
    public String deleteById(@PathVariable int id) {
        productRepository.deleteById(id);
        return "product " + id + " deleted successfully";
    }
}